using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class QuanLyKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveQuanLyKho(QuanLyKho quanlykho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddQuanLyKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddQuanLyKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, quanlykho.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdKho", DbType.Int32, ParameterDirection.Input, quanlykho.IdKho);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdHangHoa", DbType.Int32, ParameterDirection.Input, quanlykho.IdHangHoa);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoChungTuNhap", DbType.String, ParameterDirection.Input, quanlykho.SoChungTuNhap);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoChungTuXuat", DbType.String, ParameterDirection.Input, quanlykho.SoChungTuXuat);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, quanlykho.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all QuanLyKho in DB
		public ResultObj GetAllQuanLyKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<QuanLyKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllQuanLyKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<QuanLyKho>();
			QuanLyKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new QuanLyKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdKho = Convert.ToInt32(reader["IdKho"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.SoChungTuNhap = Convert.ToString(reader["SoChungTuNhap"]);
			item.SoChungTuXuat = Convert.ToString(reader["SoChungTuXuat"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all QuanLyKho in DB with condition
		public ResultObj GetAllQuanLyKhoByID(QuanLyKho quanlykho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<QuanLyKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllQuanLyKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, quanlykho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<QuanLyKho>();
			QuanLyKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new QuanLyKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdKho = Convert.ToInt32(reader["IdKho"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.SoChungTuNhap = Convert.ToString(reader["SoChungTuNhap"]);
			item.SoChungTuXuat = Convert.ToString(reader["SoChungTuXuat"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a QuanLyKho out of DB with condition
		public ResultObj DeleteQuanLyKho(QuanLyKho quanlykho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteQuanLyKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteQuanLyKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, quanlykho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of HangHoa from DB with condition
		public ResultObj GetHangHoa(int IdHangHoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<HangHoa> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetHangHoa", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdHangHoa);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<HangHoa>();
			HangHoa item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new HangHoa();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ChuoiTimKiem = Convert.ToString(reader["ChuoiTimKiem"]);
			item.NoiLuuTru = Convert.ToString(reader["NoiLuuTru"]);
			item.IdKhayDung = Convert.ToInt32(reader["IdKhayDung"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of Kho from DB with condition
		public ResultObj GetKho(int IdKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<Kho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetKho", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<Kho>();
			Kho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new Kho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenKho = Convert.ToString(reader["TenKho"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}

        public ResultObj GetAllTheKho()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<TheKho> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllTheKho", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<TheKho>();
                TheKho item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new TheKho();                    
                    item.SoChungTuNhap = Convert.ToString(reader["SoChungTuNhap"]);
                    item.SoChungTuXuat = Convert.ToString(reader["SoChungTuXuat"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
                    item.MaHang = Convert.ToString(reader["MaHang"]);
                    item.SoLuongNhap = Convert.ToInt64(reader["SoLuongNhap"]);
                    item.SoLuongXuat = Convert.ToInt64(reader["SoLuongXuat"]);
                    item.DienGiai = Convert.ToString(reader["DienGiai"]);
                    item.TenChiNhanh = Convert.ToString(reader["TenChiNhanh"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        public ResultObj GetAllBCDXNT()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<BangCanDoiXuatNhapTon> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllBCDXNT", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<BangCanDoiXuatNhapTon>();
                BangCanDoiXuatNhapTon item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new BangCanDoiXuatNhapTon();
                    item.mavattu = Convert.ToString(reader["MaHang"]);
                    item.tenvattu = Convert.ToString(reader["TenHang"]);
                    item.dvt = Convert.ToString(reader["DonViTinh"]);
                    item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);                 
                    item.sln = Convert.ToInt64(reader["SoLuongNhap"]);
                    item.slx = Convert.ToInt64(reader["SoLuongXuat"]);
                    item.IdChiNhanh = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
                    item.gtn = Convert.ToInt64(reader["ThanhTienNhap"]);
                    item.gtx = Convert.ToInt64(reader["ThanhTienXuat"]);
              
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }
    }
}
